import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, ValidationErrors, ValidatorFn, Validators, FormControl } from '@angular/forms';
import { Router } from '@angular/router';
import { PasswordResetService } from './service/password-reset.service';
import { I18nService } from '../../../core/i18n/i18n.service';
import { RegionNumber } from 'src/app/model';
import { NzMessageService } from 'ng-zorro-antd';
import { MemberService } from '../../member-center/member.server';


@Component({
  selector: 'app-password-reset',
  templateUrl: './password-reset.component.html',
  styleUrls: ['./password-reset.component.scss'],
  providers: [
    PasswordResetService
  ]
})
export class PasswordResetComponent implements OnInit {

  // @Input()
  set current(_current: number) {
    if (_current >= 2) {
        this.resetPassword();
    } else {
      this._current = _current;
    }
  }

  get current(): number {
    return this._current;
  }



  constructor(
    private fb: FormBuilder,
    private router: Router,
    private passwordResetService: PasswordResetService,
    private i18nService: I18nService,
    private memberServer: MemberService,
  ) {
    this.resetPwsForm = this.fb.group({
      email: [null, [Validators.required, Validators.pattern('^[a-zA-Z0-9_\-]{2,}@[a-zA-Z0-9_\-]{2,}(\.[a-zA-Z0-9_\-]+){1,2}$')]],
      newPwd: [null, [Validators.required]],
      verifyCode: [null, [Validators.required]],
      newConfirmPwd: [null, [Validators.required, this.confirmationValidator]],
    });

  }

  private _current = 0;

  nzSelectedIndex = 0;
  regionNumbers: RegionNumber[] = [];

  resetPwsForm: FormGroup;


  authCodeLable = '发送验证码';
  authCodeLableType = false;


  formNext(num: number) {
  }

  toPage(url: string) {
    this.router.navigateByUrl(url);
  }
  getAuthCodeLable() {
    this.authCodeLableType = true;
    if (this.authCodeLable === '发送验证码' || this.authCodeLable === '重新发送' ) {
      let num = 60;
      this.authCodeLable = '重新发送(' + num + 's)';
      const timmer = setInterval(() => {
          num--;
        this.authCodeLable = '重新发送(' + num + 's)';
      }, 1000);
      setTimeout(() => {
        clearInterval(timmer);
        this.authCodeLableType = false;
        this.authCodeLable = '重新发送';
      }, 20 * 1000);
    }
  }

  changeLang(lang: string) {
    this.i18nService.changeLang(lang);
  }

  private getVerifyCode(data) {
    this.toGetVerifyCode(data);
  }
  private resetPassword() {
  }

  private confirmationValidator = (control: FormControl): { [ s: string ]: boolean } => {
    if (!control.value) {
      return { required: true };
    } else if (control.value !== this.resetPwsForm.controls.newPwd.value) {
      return { confirm: true, error: true };
    }
  }

  async toGetVerifyCode(data) {
    const tag = await this.memberServer.getAJX('重置登入密码时(未登入) 发送验证码', data);
    this.memberServer.getApiSuccess(tag, {bool: true, msg: '验证码发送成功，请注意查收！'}) ? this.successListOk(tag, '重置登入密码时(未登入) 发送验证码') : '';
  }
  async submit(data) {
    const tag = await this.memberServer.getAJX('商户重置登入密码(未登陆)', data);
    this.memberServer.getApiSuccess(tag, {bool: true, msg: '密码重置成功，请重新登陆！'}) ? this.successListOk(tag, '商户重置登入密码(未登陆)') : '';
  }

  public successListOk(res, url): void {
    switch (url) {
      case '重置登入密码时(未登入) 发送验证码':
        this.getAuthCodeLable();
        break;
      case '商户重置登入密码(未登陆)':
        this.router.navigate(['pages/login']);
        break;
      }
    }

  ngOnInit() {
  }
}
